package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.entity.FoodMonthSale;
import com.woniuxy.dao.FoodMonthSaleMapper;
import com.woniuxy.entity.StoreInfo;
import com.woniuxy.service.FoodMonthSaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.FoodVarietyService;
import com.woniuxy.service.StoreInfoService;
import com.woniuxy.service.dto.FoodSaleDto;
import com.woniuxy.service.dto.StoreSaleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜品月销量表 服务实现类
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */
@Service
public class FoodMonthSaleServiceImpl extends ServiceImpl<FoodMonthSaleMapper, FoodMonthSale> implements FoodMonthSaleService {

    @Autowired
    private FoodVarietyService foodVarietyService;
    @Autowired
    private StoreInfoService storeInfoService;
    //获取当前商家上月销量前5菜品
    @Override
    public List<FoodSaleDto> showFoodSale(Long accountId) {
        QueryWrapper<StoreInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accountId",accountId);
        StoreInfo storeInfo = storeInfoService.getOne(queryWrapper);
        Long storeId = storeInfo.getId();
        QueryWrapper<FoodMonthSale> wrapper = new QueryWrapper<>();
        wrapper.eq("storeId",storeId);
        LocalDate localDate = LocalDate.now();
        String dateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy年MM月"));
        wrapper.eq("dateStr",dateStr);

        wrapper.orderByDesc("count");
        List<FoodMonthSale> list = list(wrapper);
        List<FoodSaleDto> dtoList = new ArrayList<>();
        if (list.size()==0){
            return null;
        }
        int a = Math.min(list.size(), 5);
        for ( int i = 0 ;i < a; i++){
            FoodSaleDto dto = new FoodSaleDto();
            dto.setFoodName(foodVarietyService.getById(list.get(i).getFoodId()).getFoodName());
            dto.setCount(list.get(i).getCount());
            dtoList.add(dto);
        }

        return dtoList;
    }

    @Override
    public List<StoreSaleDto> showStoreSale(Long accountId) {
        List<StoreSaleDto> storeSaleDtos = new ArrayList<>();
        QueryWrapper<StoreInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accountId",accountId);
        StoreInfo storeInfo = storeInfoService.getOne(queryWrapper);
        Long storeId = storeInfo.getId();
        for (int i = 0 ; i < 3 ; i++){
            Integer count = 0;
            QueryWrapper<FoodMonthSale> wrapper = new QueryWrapper<>();
            wrapper.eq("storeId",storeId);
            LocalDate localDate = LocalDate.now().minusMonths(i);
            String dateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy年MM月"));
            wrapper.eq("dateStr",dateStr);
            List<FoodMonthSale> list = list(wrapper);
            for (FoodMonthSale foodMonthSale : list) {
                count = foodMonthSale.getCount()+count;
            }
            StoreSaleDto storeSaleDto = new StoreSaleDto();
            storeSaleDto.setCount(count);
            storeSaleDto.setDateStr(dateStr);
            storeSaleDtos.add(storeSaleDto);
        }

        return storeSaleDtos;
    }
}
